/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet.server;

import java.io.IOException;
import java.sql.Timestamp;

import lineage.core.Opcodes;
import lineage.model.instance.L1PcInstance;
import lineage.packet.L1AbstractPacketServer;

/**
 * 角色信息(角色选择界面).
 * 
 * @version 2014年3月19日下午11:32:34
 * @author jrwz
 */
public final class S_CharSelectInfo extends L1AbstractPacketServer {
    private byte[] _byte = null;

    /**
     * 角色信息(角色选择界面).
     * 
     * @param pc
     *            - 角色
     */
    public S_CharSelectInfo(final L1PcInstance pc) {
        writeC(Opcodes.S_OPCODE_CHAR_LIST);
        writeS(pc.getName()); // 角色名称
        writeS(pc.getClanName()); // 血盟名称
        writeC(pc.getType()); // 职业
        writeC(pc.getSex()); // 性别
        writeH(pc.getLawful()); // 正义值
        writeH(pc.getHp()); // 血量
        writeH(pc.getMp()); // 魔量
        writeC(pc.getAc()); // 物理防御
        writeC(pc.getLevel()); // 级别
        writeC(pc.getStr()); // 力量值
        writeC(pc.getDex()); // 敏捷值
        writeC(pc.getCon()); // 体质值
        writeC(pc.getWis()); // 精神值
        writeC(pc.getCha()); // 魅力值
        writeC(pc.getInt()); // 智力值
        writeC(0x00); // 权限 (大于0为GM?)

        final int[] birthday = getBirthday(pc.getCreateTime()); // 生日(Timestamp对象)
        writeC(birthday[0]);
        writeC(birthday[1]);
        writeC(birthday[2]);
        writeC(birthday[3]);

        writeC(0x00);
    }

    /**
     * 取得生日.
     * 
     * @param ts
     *            - 生日(Timestamp对象)
     * @return 当前日期十六进制的无符号整数形式(int型数组)
     */
    private int[] getBirthday(final Timestamp ts) {
        final String date = ts.toString().substring(0, 10);
        final int time = Integer.parseInt(date.replace("-", ""));

        String times = Integer.toHexString(time);
        if (times.length() < 8) {
            times = "0" + times;
        }

        final int[] result = new int[4];
        result[0] = Integer.decode("0x" + times.substring(6, 8));
        result[1] = Integer.decode("0x" + times.substring(4, 6));
        result[2] = Integer.decode("0x" + times.substring(2, 4));
        result[3] = Integer.decode("0x" + times.substring(0, 2));
        return result;
    }

    @Override
    public byte[] getContent() throws IOException {
        if (this._byte == null) {
            this._byte = this.getBytes();
        }
        return this._byte;
    }
}
